<?php
$conn = mysql_connect("localhost", "root", "643313") or die(mysql_error());
mysql_select_db("regist_sys");

$sql_degree = "SELECT * FROM degree";
$result_degree = mysql_query($sql_degree, $conn) or die(mysql_error());

$sql_dep2 = "SELECT * FROM department";
$result_dep2 = mysql_query($sql_dep2,$conn) or die(mysql_error());

$sql_dep = "SELECT * FROM department";
$result_dep = mysql_query($sql_dep,$conn) or die(mysql_error());
?>
<?php
$sql_research = "SELECT * FROM research";
$result_research = mysql_query($sql_research,$conn) or die(mysql_error());
$num = mysql_num_rows($result_research);

echo "<script language=javascript> \n";

echo "department = new Array(); \nresearch = new Array();\n";
$i = 0;
while ($row = mysql_fetch_assoc($result_research)) {
	echo "department[$i]='$row[department]';\n";
	echo "research[$i]='$row[research]';\n";
	$i++;
}
echo "</script> \n";
?>
<script type="text/javascript" language="javascript">
function depChange(dep) {
	<?php echo "var n = $num;\n";?>
	formp.research.length = 1;
	//var newOption = document.createElement("option");
	for (var i=0;i<n;i++) {
		if (department[i]==dep) {
			var newOption = document.createElement("option");
			  newOption.text = research[i];
			  newOption.value = research[i];
			  var sel = document.getElementById("research");
			  try {
				    sel.add(newOption, null); // standards compliant; doesn't work in IE
				  }
				  catch(ex) {
				    sel.add(newOption); // IE only
				  }
		}
	}
}
</script>
<script type="text/javascript">
<!--

//-->
var myValue = 0;
function shows() {
	window.myValue = 0;
	sr = document.getElementById("s");
	pr = document.getElementById("p");
	sr.style.display = '';
	pr.style.display = 'none';
	sr.checked = true;
	pr.checked = false;
}
function showp() {
	window.myValue = 1;
	sr = document.getElementById("s");
	pr = document.getElementById("p");
	sr.style.display = 'none';
	pr.style.display = '';
	pr.checked = true;
	sr.checked = false;
}
</script>
<div id="main">
	<input type="radio" name="radio" onclick="javascript:shows();" checked />Student
	<input type="radio" name="radio" onclick="javascript:showp();" />Professor
	<br>
	<br>
	<div id="s">
		<form action="searchstudent.php" name="forms">
			<fieldset>
				<legend>Search Student</legend>
				<table>
					<tr>
						<td><label>ID</label></td>
						<td><input type="text" name="sid" /></td>
					</tr>
					<tr>
						<td><label>First Name</label></td>
						<td><input type="text" name="sfirstName" /></td>
					</tr>
					<tr>
						<td><label>Last Name</label></td>
						<td><input type="text" name="slastName" /></td>
					</tr>
					<tr>
						<td><label>Degree</label></td>
						<td><select name="sdegree">
						<option></option>
						<?php
						while ($row = mysql_fetch_assoc($result_degree)) {
							foreach ($row as $deg => $value) {
								print "<option>$value</option> \n";
							}
						}
						?>
						</select>
						</td>
					</tr>
					<tr>
						<td>
							<p>
								<label>Department</label>
							</p>
						</td>
						<td><p>
								<select name="sdepartment">
								<option></option>
								<?php
								while ($row = mysql_fetch_assoc($result_dep2)) {
									print "<option>";
									print $row["department"];
									print "</option> \n";
								}
								?>
								</select>
							</p>
						</td>
					</tr>
					<tr>
						<td><p>
								<button type="submit">Submit</button>
								<button type="reset">Reset</button>
							</p>
						</td>
					</tr>
				</table>
			</fieldset>
		</form>
	</div>
	<div id="p" style="display: none">
		<form action="searchprofessor.php" name="formp">
			<fieldset>
				<legend>Search Professor</legend>
				<table>
					<tr>
						<td><label>ID</label></td>
						<td><input type="text" name="pid" /></td>
					</tr>
					<tr>
						<td><label>First Name</label>
						</td>
						<td><input type="text" name="pfirstName" />
						</td>
					</tr>
					<tr>
						<td><label>Last Name</label></td>
						<td><input type="text" name="plastName" />
						</td>
					</tr>
					<tr>
						<td><label>Department</label></td>
						<td><select name="department"
							onchange="depChange(this.options[this.selectedIndex].text);">
								<option></option>
								<?php
								while ($row = mysql_fetch_assoc($result_dep)) {
									print "<option>";
									print $row["department"];
									print "</option> \n";
								}
								?>
						</select>
						</td>
					</tr>
					<tr>
						<td><label>Research Area</label>
						</td>
						<td><select id="research" name="research">
						<option></option>
						</select>
						</td>
					</tr>
					<tr>
						<td><p>
								<button type="submit">Submit</button>
								<button type="reset">Reset</button>
							</p>
						</td>
					</tr>
				</table>
			</fieldset>
		</form>
	</div>
</div>
